//index.js
//获取应用实例
var api = require("../../api.js"),
  r = "",
  app = getApp();

Page({
  data: {
    /** 默认选中的tab */
    active: 0,
    tabbar: {},
    pageIndex: 1,
    loading: false, //第一次加载，设置true  
    loadingComplete: false, //把“没有数据”设为false，隐藏  
    isFromSearch: true,
    is_vip: 0 ,/** 非会员 */
    mine_data:[], /** 我的班级 */
    recommend_data: [] /** 推荐班级 */
  },
  onLoad: function(options) {
    r = this;
    app.editTabbar();
    if (options.shareType) {
      app.globalData.shareType = options.shareType;
      app.globalData.userId = options.id;
    }
    app.getWxUserInfo().then(res => {
      app.globalData.shareType = '';
      app.globalData.userId = '';
      if (options.id) {
        setTimeout(() => {
          wx.navigateTo({
            url: `../discoverdetail/discoverdetail?id=${options.id}`
          });
        }, 800);
      }
      r.initData(); //初始化数据
    })
  },
  onReady: function() {
    wx.hideTabBar({
      fail: function() {
        setTimeout(function() {
          wx.hideTabBar();
        }, 500);
      }
    });
  },
  /** 跳转到详情页面 */
  gotail: function(e) {
    let _this = this;
    /** 是会员可以查看内容 */
    console.log('_this.data.is_vip -- > ' + _this.data.is_vip)
    if (_this.data.is_vip == 1) {
      var o = e.currentTarget.dataset;
      wx.navigateTo({
        url: "../discoverdetail/discoverdetail?id=" + o.orderid
      });
    } else {
      wx.showToast({
        title: "BFC用户可查看",
        image: "../../images/un_see.png",
        duration:2000
      });
    }
  },
  /** 判断是否为高级会员用户 */
  is_vip: function() {
    let that = this;
    var open_id = getApp().globalData.wxUserInfo.openId;
    /** 用户不为空（已登陆） */
    if (open_id) {
      var n = new Object();
      n.open_id = open_id
      wx.request({
        url: api.wx.is_vip,
        data: n,
        success: function(res) {
          /** 非会员不可查看此内容 */
          if (res.data.data == 0) {
            /** 设置非会员 */
            that.setData({ is_vip: 0, });;
          } else {
            /** 设置会员 */
            that.setData({ is_vip: 1, });
          }
          console.log('that.data.is_vip -- > ' + that.data.is_vip)
        }
      });
    }
  },
  /** 初始化数据 */
  initData: function() {
    /** 判断用户是否 是 高级会员用户*/
    this.is_vip();
    this.mine_data();
    this.recommend_data();
  },
  /** 我的 数据 */
  mine_data:function(){
    /** 数据条件 */
    var t = new Object();
    t.openId = getApp().globalData.wxUserInfo.openId;
    wx.request({
      url: api.audio.getWorkShops,
      data: t,
      success: function (res) {
        if (res.statusCode == 200 && res.data.code == 200) {
            var n = res.data.data;
            if (n.length != 0) {
              var checkStatus = '';
              /** 是否停止分页加载 */
              n.length < 10 ? checkStatus = false : true;
              setTimeout(() => {
                r.setData({
                  mine_data: n, /** 赋值 */
                  loading: checkStatus,
                  loadingComplete: !checkStatus,
                  hidden: false
                });
              }, 500)
            } else {
              setTimeout(() => {
                r.setData({
                  loadingComplete: false,
                  loading: false,
                  hidden: true
                })
              }, 500)
            }
          }
      }
    });
  },

  /** 推荐 数据 */
  recommend_data: function () {
    /** 数据条件 */
    var t = new Object();
    t.pageIndex = 1, t.pageSize = 10;
    wx.request({
      url: api.audio.getWorkShops,
      data: t,
      success: function (res) {
        if (res.statusCode == 200 && res.data.code == 200) {
          var n = res.data.data;
          if (n.length != 0) {
            var checkStatus = '';
            /** 是否停止分页加载 */
            n.length < 10 ? checkStatus = false : true;
            setTimeout(() => {
              r.setData({
                recommend_data: n, /** 赋值 */
                loading: checkStatus,
                loadingComplete: !checkStatus,
                hidden: false
              });
            }, 500)
          } else {
            setTimeout(() => {
              r.setData({
                loadingComplete: false,
                loading: false,
                hidden: true
              })
            }, 500)
          }
        }
      }
    });
  },

  // 加载分页数据
  loading() {
    var t = new Object();
    var dataList = [];
    t.pageIndex = r.data.pageIndex, t.pageSize = 10;
    wx.request({
      url: api.audio.getWorkShops,
      data: t,
      success: function(res) {
        if (res.statusCode == 200) {
          if (res.data.code == 200) {
            var n = res.data.data;
            if (n.length > 0) {
              r.data.isFromSearch ? dataList = n : dataList = r.data.data.concat(n)
              setTimeout(() => {
                r.setData({
                  data: dataList,
                  loading: true,
                });
              }, 500)
            } else {
              setTimeout(() => {
                r.setData({
                  loadingComplete: true,
                  loading: false,
                })
              }, 500)
            }
          }
        }
      }
    });
  },
  /**  页面相关事件处理函数--监听用户下拉动作  */
  onPullDownRefresh: function() {
    wx.showNavigationBarLoading(),
      console.log("下拉刷新"),
      setTimeout(function() {
        wx.hideNavigationBarLoading(), wx.stopPullDownRefresh();
      }, 1500);
  },
  /** 页面上拉触底事件的处理函数 */
  onReachBottom: function() {
    let _that = this
    if (_that.data.loading && !_that.data.loadingComplete) {
      _that.setData({
        pageIndex: _that.data.pageIndex + 1,
        isFromSearch: false
      });
      _that.loading();
    };
  },

});